import React, { Component } from "react";
import { connect } from "react-redux";
import MainLayout from "../../../../component/mainLayout/mainLayout";
import { getAppPlatformsDatas } from "../../../../Redux/Action/nativeDatas/nativeDatas";
import { setUserType } from "../../../../Redux/Action/credit/AuthenticationA";
import "./index.scss";
import yhTools from "../../../../tools/yh";
import { InterfaceG } from "@config/Config";
import { Toast, Modal } from "antd-mobile";
import InviteModal from "./components/inviteModal";

class inviteHome extends Component {
	constructor(props) {
		super(props);
		this.state = {
			fromPage: "" //从...页进来
		};
	}

	componentDidMount() {
		const fromPage = sessionStorage.getItem("inviteFromPage");
		this.setState({
			visible: true,
			fromPage: fromPage || ""
		});
		this.credit_queryCreditApplyStatus();
	}

	credit_queryCreditApplyStatus = () => {
		const objs = {
			url: InterfaceG.credit_queryCreditApplyStatus,
			params: { pruductNum: "601" }
		};
		yhTools.request(objs).then(res => {
			Toast.hide();
			if (res.code === "0") {
				if (res.data) {
					const { applyStatus } = res.data;
					//0未授信，1已授信，2授信中，4授信拒绝
					let pathName = "/";
					if (applyStatus === "0") {
						pathName = "/xhfHomeNoCredit";
					} else if (applyStatus === "1") {
						pathName = "/xhfHomeCredit";
					} else if (applyStatus === "2") {
						pathName = "/xhfHomeCrediting";
					} else if (applyStatus === "4") {
						pathName = "/xhfHomeNoCredit";
					}
					this.setState({
						pathName
					});
				}
			} else {
				Toast.info(res.message, 2);
			}
		});
	};

	//邀请
	invite(type) {
		let objs = {
			url: InterfaceG.inviteShare,
			params: {},
			_alertShow: false
		};
		Toast.loading("加载中...", 100);
		yhTools.request(objs).then(res => {
			if (res.code == "0") {
				let { couponValue, shareTextRespList, shareUrl, userName } = res.data;
				if (type == 1) {
					//立即邀请
					this.inviteModal.setInviteInfo({
						couponValue,
						shareTextRespList,
						shareUrl,
						userName
					});
					this.inviteModal.open();
				} else {
					//二维码邀请
					sessionStorage.setItem("inviteUrl", shareUrl);
					this.props.history.push({ pathname: "/inviteQrCode" });
				}
			} else if (res.code == "70005") {
				Modal.alert("邀请提示", "请先完成小辉付开通，才可邀请好友获得奖励哦~", [
					{
						text: "去开通",
						onPress: () => {
							this.props.history.push({
								pathname: "/xhfHomeNoCredit",
								query: {
									fromPage: "invite"
								}
							});
						}
					},
					{ text: "取消" }
				]);
			} else {
				Toast.info(res.message);
			}
		});
	}
	render() {
		const { pathName, fromPage } = this.state;
		const _pageParams = {
			header: {
				title: "",
				goBackAction: () => {
					let path = "";
					switch (fromPage) {
						case "xhf":
							path = pathName;
							break;
						case "/paymentDiscount/index":
							path = "/paymentDiscount/index";
							break;

						default:
							path = "/xhqbIndex";
							break;
					}
					sessionStorage.removeItem("inviteFromPage");
					this.props.history.push(path);
				},
				rightButt: "活动规则",
				rightButtCallBack: () => {
					this.props.history.push("inviteRule");
				}
			},
			history: this.props.history,
			cacheLifecycles: this.props.cacheLifecycles
		};

		return (
			<MainLayout pageParams={_pageParams}>
				<div className="invite_home">
					<div className="invite_box">
						<div className="invite_btn_box">
							<div
								className="btn_now_invite"
								onClick={() => {
									this.invite(1);
								}}
							>
								立即邀请
							</div>
							<div
								className="btn_qr_invite"
								onClick={() => {
									this.invite(2);
								}}
							>
								二维码邀请
							</div>
						</div>
					</div>
					<div className="record">
						<div
							className="btn"
							onClick={() => {
								this.props.history.push("/rewardRecord");
							}}
						>
							奖励记录{">"}
						</div>
						<div
							className="btn"
							onClick={() => {
								this.props.history.push("/inviteRecord");
							}}
						>
							邀请记录{">"}
						</div>
					</div>

					<InviteModal
						onRef={ref => {
							this.inviteModal = ref;
						}}
					></InviteModal>
				</div>
			</MainLayout>
		);
	}
}

function mapStateToProps(state) {
	return state;
}

function mapDispatchToProps(dispatch) {
	return {
		_getAppPlatformsDatas: options => {
			// 同步数据
			dispatch(getAppPlatformsDatas(options));
		},
		_setUserType: options => {
			dispatch(setUserType(options));
		}
	};
}

export default connect(mapStateToProps, mapDispatchToProps)(inviteHome);
